namespace FIREFLYX {

    /** 后处理效果  色调 */
    export class PostEffectColorTone implements IExample {
        public Init(canvas: HTMLCanvasElement): void {
            console.log("PostEffectColorTone");
            let gl = canvas.getContext("webgl2");
            //app init
            App.init(gl.canvas as any);
            //简单观察相机
            let cVEnv = GenCamViewEnv();
            let cam = cVEnv.camera;
            //灯光
            const light = GenMainLight(45);

            //添加 后处理效果 组件
            let colorToneEffComp = cam.entity.addComponent<ColorToneEff>(ColorToneEff);
            colorToneEffComp.setColor(0.5, 0, 0.6);

            //模型节点
            let model = GameObjectUtil.createPrimitive(GameObjectPrimitiveType.CYLINDER);
            model.transform.scale = new Vector3(2, 2, 2);

        }
    }

    /** 实现 colorTone 效果的脚本组件 */
    class ColorToneEff extends Script {
        private _mtr: Material;

        private _color: Color4 = new Color4(1, 1, 1, 1);

        /**
         * 设置映射颜色
         * @param r 
         * @param g 
         * @param b 
         * @returns 
         */
        public setColor(r: number, g: number, b: number) {
            this._color.set(r, g, b, 1.0);
            if (!this._mtr) return;
            this._mtr.setProperty("u_colorTone", this._color);
        }

        private makeShader() {
            //shader
            //后处理 顶点程序
            let postEffVS = `#version 300 es
             layout (location = 0) in vec4 a_Position; //顶点 位置 是有默认值的 (0,0,0,1)
             layout (location = 3) in vec2 a_UV;       //UV 纹理坐标
             out vec2 v_uv;
             void main() {
                 v_uv = a_UV;
                 vec4 pos = vec4(a_Position.xyz , 1.0);
                 gl_Position = pos;
             }
         `;

            //后处理 片源程序
            let postEffFS = `#version 300 es
             precision highp float;
             uniform sampler2D u_mainTex;
             uniform vec4 u_colorTone;
             in vec2 v_uv;
             out vec4 color;
             void main(){
                 vec4 texColor = texture(u_mainTex , v_uv);    //纹理颜色
                 color = texColor * u_colorTone;
                 color.w =1.0;
                 // color = vec4(1.,1.,1.,1.);
             }
         `;

            //glsl
            const vs = new GLSL();
            vs.setCode(postEffVS);
            const fs = new GLSL();
            fs.setCode(postEffFS);

            //shader
            let result: Shader = new Shader();
            result.lightMode = RenderPipeline.LM_FORWARD;
            result.vertexGLSL = vs;
            result.fragmentGLSL = fs;
            //unifrom 默认值
            result.definePropertyMap = {};

            result.definePropertyMap.u_mainTex = {
                type: "texture",
                value: "White",
            };

            result.definePropertyMap.u_colorTone = {
                type: "vector4",
                value: [1, 1, 1, 1],
            };

            //apply
            result.apply();

            return result;
        }

        public start() {
            let sh = this.makeShader();
            this._mtr = new Material("colorTone");
            this._mtr.shader = sh;
            this._mtr.setProperty("u_colorTone", this._color);
        }

        public onRenderImage(src: RenderTexture2D, dest: RenderTexture2D) {
            //材质设置颜色 
            //渲染
            const mtr = this._mtr;
            Graphics.Blit(src, dest, mtr);  //直接从src 绘制到 dest
        }
    }
}